﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.Config;
using Xiaov.SmartWeb.Model;
using Xiaov.SmartWeb.Model.ActiveRecord;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using Xiaov.SmartWeb.Model.Domain;

namespace Xiaov.SmartWeb.Application.SysAdmin
{
    public partial class Admin_Plugins : BasePage
    {
        public string unzipdir { get; set; }

        public string zipname { get; set; }

        public string password { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //权限横切
                //PM_FilterByProperty(ModulePropoty.MODULEURL, Request.Url.AbsolutePath);
                PM_Filter();
                AjaxPro.Utility.RegisterTypeForAjax(typeof(Admin_Plugins));
            }
        }

        /// <summary>
        /// 安装插件
        /// </summary>
        /// <param name="key"></param>
        [AjaxPro.AjaxMethod]
        public void Install(string key)
        {
            this.unzipdir = Server.MapPath("../");
            this.zipname = key;
            Unzip();

            //读取配置文件
            String[] arr = System.IO.File.ReadAllLines(key.Replace(".zip", ".txt"));
            S_M_PLUGIN plugin = new S_M_PLUGIN();
            plugin.PLUGINID = Guid.NewGuid().ToString();
            plugin.PLUGINNAME = arr[0];
            plugin.PLUGINAUTHOR = arr[1];
            plugin.PLUGINPATH=arr[2];
            plugin.PLUGINSTATE = "1";
            plugin.PLUGINVERSION = arr[3];
            plugin.PLUGINDLL = arr[4];
            plugin.Save();
        }

        /// <summary>
        /// 卸载插件
        /// </summary>
        /// <param name="key"></param>
        [AjaxPro.AjaxMethod]
        public bool UnInstall(string key)
        {
            //
            bool rbool = false;
            S_M_PLUGIN plugin = S_M_PLUGIN.Find(key);
            if (plugin != null)
            {
                try
                {
                    string path = Server.MapPath(plugin.PLUGINPATH);
                    if (System.IO.Directory.Exists(path))
                    {
                        System.IO.Directory.Delete(path, true);
                        DeleteFiles(Server.MapPath("/bin/"), plugin.PLUGINDLL);
                    }
                    plugin.Delete();
                    rbool = true;

                }
                catch
                {
                    rbool = false;
                }
            }
            return rbool;
        }

        /// <summary>
        /// 删除指定前缀名称的文件
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="preName">前缀名称</param>
        public bool DeleteFiles(string path,string preName)
        {
            bool rbool = false;
            try
            {
                FileView fv = new FileView();
                List<FileView> list = new List<FileView>();
                list = fv.GetFileListByPreName(path, preName);
                foreach (var q in list)
                {
                    if (System.IO.File.Exists(q.FilePath))
                    {
                        System.IO.File.Delete(q.FilePath);
                    }
                }
                rbool = true;
            }
            catch
            {
                rbool = false;
            }
            return rbool;
        }

        /// <summary>
        /// 解压文件
        /// </summary>
        public void Unzip()
        {
            string unzipdirectoryroot = this.unzipdir;
            // 调用组件中读取压缩文件的对象 
            ZipInputStream s = new ZipInputStream(File.OpenRead(this.zipname));
            s.Password = this.password;
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                // 当前解压到的目录，Path.GetDirectoryName(theEntry.Name)为提取压缩文件目录 
                string directoryName = Path.Combine(unzipdirectoryroot, Path.GetDirectoryName(theEntry.Name));
                // 当前正在解压的文件 
                string fileName = Path.Combine(directoryName, Path.GetFileName(theEntry.Name));
                // 创建目录 
                Directory.CreateDirectory(directoryName);
                if (fileName != String.Empty)
                {
                    try
                    {
                        // 创建解压文件 
                        FileStream streamWriter = File.Create(fileName);
                        // 将数据写入文件 
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                    catch
                    {
                        
                    }

                }
            }
            s.Close();
        }

    }

   
}